﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebOS.Bll;
using WebOS.Dal;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebOSCore.Controllers.Api
{

    public class WorkFlowController : Controller
    {


        [Route("api/WorkFlow/WorkFlowList")]
        [HttpGet]
        [Authorize]
        public string WorkFlowList(string title, Guid type, DateTime date1, DateTime date2, int page, int limit)
    {
            //根据用户获取待办
            var user = User.Claims.FirstOrDefault(c => c.Type == "id").Value;
            Guid guid;
            Guid.TryParse(user, out guid);
            return WorkFlowBll.Instance.WorkFlowList(title, type, guid, date1, date2, page, limit);
        }
        [Route("api/WorkFlow/GetJSON")]
        [HttpGet]
        [Authorize]
        public string GetJSON(Guid flowid,string type) {
           return WorkFlowBll.Instance.GetJSON(flowid,type);
        }
        //      string json = context.Request.Form["json"];

        [Route("api/WorkFlow/Save")]
        [HttpPost]
        [Authorize]
        public string Save(string json, string op)
        {
            var user = User.Claims.FirstOrDefault(c => c.Type == "id").Value;
            if (op.ToLower() == "save".ToLower()) {
                Guid guid;
                Guid.TryParse(user, out guid);
               return WorkFlowBll.Instance.Seve(json,guid);
            }
            if (op.ToLower() == "install".ToLower())
            {
                Guid guid;
                Guid.TryParse(user, out guid);
                return WorkFlowBll.Instance.Install(json, guid,false);
            }
            
            return ""; //WorkFlowBll.Instance.GetJSON(flowid, type);
        }
        [Route("api/WorkFlow/Set_Flow")]
        [HttpPost]
        [Authorize]
        public string Set_Flow() { 
             return "";
        }
        [Route("api/WorkFlow/GetTypeOptions")]
        [HttpPost]
        [Authorize]
        public string GetTypeOptions(string value = "")
        {
            return  DictionaryBll.Instance.GetOptionsByCode("FlowTypes", value);
        }
        [Route("api/WorkFlow/GetTable_SqlServer")]
        [HttpGet]
        [Authorize]
        public string GetTable_SqlServer()
        {
            return DBConnectionBll.Instance.GetTable_SqlServer();
        }
        [Route("api/WorkFlow/GetFields")]
        [HttpGet]
        [Authorize]
        public string GetFields(string table) {
          return DBConnectionBll.Instance.GetFields(table);
        }

    }
}
